package com.atguigu.yygh.user.service.impl;

import com.atguigu.client.cmn.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ybl
 * @version 1.9
 * @date 2021/5/22 10:37
 * 就诊人管理
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

  @Autowired
  private DictFeignClient dictFeignClient;

  // 根据用户id获取所有就诊人
  @Override
  public List<Patient> findAll(Long userId) {
    List<Patient> patients = baseMapper.selectList(new QueryWrapper<Patient>().eq("user_id", userId));
    // 把就诊人的信息进行封装
    patients.forEach(this::packPatient);
    return patients;
  }

  // 根据id获取就诊人信息
  @Override
  public Patient getPatientId(Long id) {
    Patient patient = baseMapper.selectById(id);
    packPatient(patient);
    return patient;
  }

  // 对就诊人信息进行封装
  private void packPatient(Patient patient) {
    //根据证件类型编码，获取证件类型具体指
    String certificatesTypeString =
        dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件
    //联系人证件类型
    String contactsCertificatesTypeString =
        dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
    //省
    String provinceString = dictFeignClient.getName(patient.getProvinceCode());
    //市
    String cityString = dictFeignClient.getName(patient.getCityCode());
    //区
    String districtString = dictFeignClient.getName(patient.getDistrictCode());
    patient.getParam().put("certificatesTypeString", certificatesTypeString);
    patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
    patient.getParam().put("provinceString", provinceString);
    patient.getParam().put("cityString", cityString);
    patient.getParam().put("districtString", districtString);
    patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
  }
}
